<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class Function_model extends CI_Model{
    
    public function __construct() {
        parent::__construct();
        $this->load->database();
        }
    public function calculateWeekday($weekday)
    {
        $nam1=  substr($weekday, 0,4);
                        $thang1=  substr($weekday,5,2);
                        $ngay1=  substr($weekday,8,2);
                        $date=$nam1.'-'.$thang1.'-'.$ngay1;
                        $nam=(int)$nam1;
                $thang=(int)$thang1;
                $ngay=(int)$ngay1;
                if($nam%400==0||($nam%4==0&&$nam%100!=0))//nam nhuan
                    {
                    $a1=$nam;
                    $a=$nam-1;
                    $b=(int)($a/4);
                    $c=(int)($a/100);
                    $d=(int)($a/400);
                    switch ($thang)
                    {
                    case 1:{$e=$ngay;break;}
                    case 2:{$e=31+$ngay;break;}
                    case 3:{$e=31+29+$ngay;break;}
                    case 4:{$e=31+29+31+$ngay;break;}
                    case 5:{$e=31+29+31+30+$ngay;break;}
                    case 6:{$e=31+29+31+30+31+$ngay;break;}
                    case 7:{$e=31+29+31+30+31+30+$ngay;break;}
                    case 8:{$e=31+29+31+30+31+30+31+$ngay;break;}
                    case 9:{$e=31+29+31+30+31+30+31+31+$ngay;break;}
                    case 10:{$e=31+29+31+30+31+30+31+31+30+$ngay;break;}
                    case 11:{$e=31+29+31+30+31+30+31+31+30+31+$ngay;break;}
                    case 12:{$e=31+29+31+30+31+30+31+31+30+31+30+$ngay;break;}
        
                        };
                        $f=$a1+$b-$c+$d+$e;
                        $g=(int)($f%7);
                        switch($g)
                        {
                              case 0: {//echo "<td class=\"row1\">Saturday</td>";
                                $stringslq="update ewt_works set weekday='Saturday' where substr(starttime,1,10)='$date'";
                                $this->db->query($stringslq);
                                break;}
                                case 1: {//echo "<td class=\"row1\">Sunday</td>";
                                $stringslq="update ewt_works set weekday='Sunday' where substr(starttime,1,10)='$date'";
                                $this->db->query($stringslq);
                                break;}
                                case 2: {//echo "<td class=\"row1\">Monday</td>";
                                $stringslq="update ewt_works set weekday='Monday' where substr(starttime,1,10)='$date'";
                                $this->db->query($stringslq);
                                break;}
                                case 3: {//echo "<td class=\"row1\">Tuesday</td>";
                                $stringslq="update ewt_works set weekday='Tuesday' where substr(starttime,1,10)='$date'";
                                $this->db->query($stringslq);
                                break;}
                                case 4: {//echo "<td class=\"row1\">Wednesday</td>";
                                $stringslq="update ewt_works set weekday='Wednesday' where substr(starttime,1,10)='$date'";
                                $this->db->query($stringslq);
                                break;}
                                case 5: {//echo "<td class=\"row1\">Thursday</td>";
                                $stringslq="update ewt_works set weekday='Thursday' where substr(starttime,1,10)='$date'";
                                $this->db->query($stringslq);
                                break;}
                                case 6: {//echo "<td class=\"row1\">Firday</td>";
                                $stringslq="update ewt_works set weekday='Firday' where substr(starttime,1,10)='$date'";
                                $this->db->query($stringslq);
                                break;}
                        };
                    }
                    else
                    {
                        $a1=$nam;
                        $a=$nam-1;
                        $b=(int)($a/4);
                        $c=(int)($a/100);
                        $d=(int)($a/400);
                        switch ($thang)
                        {
                            case 1:{$e=$ngay;break;}
                            case 2:{$e=31+$ngay;break;}
                            case 3:{$e=31+28+$ngay;break;}
                            case 4:{$e=31+28+31+$ngay;break;}
                            case 5:{$e=31+28+31+30+$ngay;break;}
                            case 6:{$e=31+28+31+30+31+$ngay;break;}
                            case 7:{$e=31+28+31+30+31+30+$ngay;break;}
                            case 8:{ $e=31+28+31+30+31+30+31+$ngay;break;}
                            case 9:{$e=31+28+31+30+31+30+31+31+$ngay;break;}
                            case 10:{$e=31+28+31+30+31+30+31+31+30+$ngay;break;}
                            case 11:{$e=31+28+31+30+31+30+31+31+30+31+$ngay;break;}
                            case 12:{$e=31+28+31+30+31+30+31+31+30+31+30+$ngay;break;}

                        };
                        $f=$a1+$b-$c+$d+$e;
                        $g=(int)($f%7);
                                switch($g)
                        {
                                case 0: {//echo "<td class=\"row1\">Saturday</td>";
                                $stringslq="update ewt_works set weekday='Saturday' where substr(starttime,1,10)='$date'";
                                $this->db->query($stringslq);
                                break;}
                                case 1: {//echo "<td class=\"row1\">Sunday</td>";
                                $stringslq="update ewt_works set weekday='Sunday' where substr(starttime,1,10)='$date'";
                                $this->db->query($stringslq);
                                break;}
                                case 2: {//echo "<td class=\"row1\">Monday</td>";
                                $stringslq="update ewt_works set weekday='Monday' where substr(starttime,1,10)='$date'";
                                $this->db->query($stringslq);
                                break;}
                                case 3: {//echo "<td class=\"row1\">Tuesday</td>";
                                $stringslq="update ewt_works set weekday='Tuesday' where substr(starttime,1,10)='$date'";
                                $this->db->query($stringslq);
                                break;}
                                case 4: {//echo "<td class=\"row1\">Wednesday</td>";
                                $stringslq="update ewt_works set weekday='Wednesday' where substr(starttime,1,10)='$date'";
                                $this->db->query($stringslq);
                                break;}
                                case 5: {//echo "<td class=\"row1\">Thursday</td>";
                                $stringslq="update ewt_works set weekday='Thursday' where substr(starttime,1,10)='$date'";
                                $this->db->query($stringslq);
                                break;}
                                case 6: {//echo "<td class=\"row1\">Firday</td>";
                                $stringslq="update ewt_works set weekday='Firday' where substr(starttime,1,10)='$date'";
                                $this->db->query($stringslq);
                                break;}
                        };
                    }
    }
    public function insertTime($userid,$numberday,$fromdate,$todate)
    {
            $sql2="select * from ewt_works where userid=$userid and starttime between '$fromdate' and '$todate' and starttime<>endtime AND (
weekday = 'Monday' OR weekday = 'Tuesday' OR weekday = 'Wednesday' OR weekday = 'Thursday' OR weekday = 'Firday') AND substr( overtime, 4, 2 ) !=0";
            $query2=$this->db->query($sql2);
            $a=0;$b=0;$c=0;$d=0;$e=0;$f=0;$g=0;
            $gio=0;
            $phut=0;
            $gio1=0;
            $phut1=0;
            foreach($query2->result() as $row2)
            {
                $phut=$phut+substr($row2->overtime,3,2);
                $gio=$gio+substr($row2->overtime,0,2);
                $phut1=$phut%60;
                $gio1=(int)($phut/60)+$gio;
            }
            $OT_Weekday=$gio1.':'.$phut1;//OT_Weekday
            $sql8="select * from ewt_works where userid=$userid and starttime between '$fromdate' and '$todate' and starttime<>endtime AND ( weekday = 'Saturday') AND substr( overtime, 4, 2 ) !=0";
            $query8=$this->db->query($sql8);
            $gio2=0;
            $phut2=0;
            $gio3=0;
            $phut3=0;
            foreach($query8->result() as $row8)
            {
                $phut2=$phut2+substr($row8->overtime,3,2);
                $gio2=$gio2+substr($row8->overtime,0,2);
                $phut3=$phut2%60;
                $gio3=(int)($phut2/60)+$gio2;
            }
            $OT_Saturday=$gio3.':'.$phut3;//OT_Saturday
            $sql9="select * from ewt_works where userid=$userid and starttime between '$fromdate' and '$todate' and starttime<>endtime AND ( weekday = 'Sunday') AND substr( overtime, 4, 2 ) !=0";
            $query9=$this->db->query($sql9);
            $gio4=0;
            $phut4=0;
            $gio5=0;
            $phut5=0;
            foreach($query9->result() as $row9)
            {
                $phut4=$phut4+substr($row9->overtime,3,2);
                $gio4=$gio4+substr($row9->overtime,0,2);
                $phut5=$phut4%60;
                $gio5=(int)($phut4/60)+$gio4;
            }
            $OT_Sunday=$gio5.':'.$phut5;//OT_Sunday
            $sql7="select count(*) as numberdayovernight from ewt_works where userid=$userid and starttime between '$fromdate' and '$todate' and starttime<>endtime and ((substr(starttime,12,2)>='21' and substr(endtime,12,2)<'24') or (substr(starttime,12,2)>'00' and substr(starttime,12,2)<'05'))";
            $query7=$this->db->query($sql7);
            foreach ($query7->result() as $row7)
            {
            //$row7->numberdayovernight;
            }
            $sql3="select count(*) as numbersunday from ewt_works where userid=$userid and starttime between '$fromdate' and '$todate' and starttime<>endtime and weekday='Sunday'";
            $query3=$this->db->query($sql3);
            foreach ($query3->result() as $row3)
            {
                //echo "<td class=\"row1\">$row3->numbersunday</td>";
            }
            $sql4="select count(*) as numbersaturday from ewt_works where userid=$userid and starttime between '$fromdate' and '$todate' and starttime<>endtime and weekday='Saturday'";
            $query4=$this->db->query($sql4);
            $hoursat=0;
            $minutesat=0;
            $hoursat1=0;
            $minutesat1=0;
            foreach ($query4->result() as $row4)
            {
                //echo "<td class=\"row1\">$row4->numbersaturday</td>"; 
            }
            $sql5="select count(*) as numbersatall from ewt_works where userid=$userid and starttime between '$fromdate' and '$todate'  and weekday='Saturday'";
            $query5=$this->db->query($sql5);
            foreach ($query5->result() as $row5)
            {
                //echo "<td class=\"row1\">$row5->numbersatall</td>";
            }
            $sql6="select count(*) as numbersundaya from ewt_works where userid=$userid and starttime between '$fromdate' and '$todate' and weekday='Sunday'";
            $query6=$this->db->query($sql6);
            foreach ($query6->result() as $row6)
            {
                //echo "<td class=\"row1\">$row3->numbersunday</td>";
            }
            $workedday=$numberday-$row3->numbersunday+$row5->numbersatall-$row4->numbersaturday;//Worked_day
            $sql30="select count(distinct substr(starttime,1,10)) as totalday from ewt_works where starttime between '$fromdate' and '$todate'";
            $query30=$this->db->query($sql30);
            foreach ($query30->result() as $row30)
            {
                
            }
            $sql31="select count(distinct substr(starttime,1,10)) as totalsunday from ewt_works where weekday='Sunday' and starttime between '$fromdate' and '$todate'";
            $query31=$this->db->query($sql31);
            foreach ($query31->result() as $row31)
            {
                
            }
            $workingday=$row30->totalday-$row31->totalsunday;
            $sql10="select * from ewt_works where userid=$userid and starttime between '$fromdate' and '$todate' and starttime<>endtime and weekday='Saturday'";
            $query10=$this->db->query($sql10);
            $hoursatturday=0;
            $minutesaturday=0;
            $hoursatturday1=0;
            $minutesaturday1=0;
            foreach($query10->result() as $row10)
            {
                //echo substr($row10->starttime,14,2);
                if(substr($row10->starttime,14,2)>substr($row10->endtime,14,2))
                {
                    $phutthu7=substr($row10->endtime,14,2)+60-substr($row10->starttime,14,2);
                    $giothu7=substr($row10->endtime,11,2)-substr($row10->starttime,11,2)-1;
                }
                else
                {
                    $phutthu7=substr($row10->endtime,14,2)-substr($row10->starttime,14,2);
                    $giothu7=substr($row10->endtime,11,2)-substr($row10->starttime,11,2);
                }
                
                $minutesaturday=$minutesaturday+$phutthu7;
                $hoursatturday=$hoursatturday+$giothu7;
                $minutesaturday1=$minutesaturday%60;
                $hoursatturday1=(int)($minutesaturday/60)+$hoursatturday;
            }
            if($minutesaturday1<$phut3)
            {
                $minutesatday=$minutesaturday1+60-$phut3;
                $hoursatday=$hoursatturday1-1-$gio3;
            }
            else
            {
                $minutesatday=$minutesaturday1-$phut3;
                $hoursatday=$hoursatturday1-$gio3;
            }
            $timesaturday=$hoursatday.':'.$minutesatday;//Time_Saturday
            $sql11="select * from ewt_works where userid=$userid and starttime between '$fromdate' and '$todate' and starttime<>endtime and weekday='Sunday'";
            $query11=$this->db->query($sql11);
            $hoursunday=0;
            $minutesunday=0;
            $hoursunday1=0;
            $minutesunday1=0;
            foreach($query11->result() as $row11)
            {
                //echo substr($row11->starttime,14,2);
                if(substr($row11->starttime,14,2)>substr($row11->endtime,14,2))
                {
                    $phutcn=substr($row11->endtime,14,2)+60-substr($row11->starttime,14,2);
                    $giocn=substr($row11->endtime,11,2)-substr($row11->starttime,11,2)-1;
                }
                else
                {
                    $phutcn=substr($row11->endtime,14,2)-substr($row11->starttime,14,2);
                    $giocn=substr($row11->endtime,11,2)-substr($row11->starttime,11,2);
                }
                
                $minutesunday=$minutesunday+$phutcn;
                $hoursunday=$hoursatturday+$giocn;
                $minutesunday1=$minutesunday%60;
                $hoursunday1=(int)($minutesunday/60)+$hoursunday;
            }
            if($minutesunday1<$phut5)
            {
                $minutesuday=$minutesunday1+60-$phut5;
                $hoursuday=$hoursunday1-1-$gio5;
            }
            else
            {
                $minutesuday=$minutesunday1-$phut5;
                $hoursuday=$hoursunday1-$gio5;
            }
            $timesunday=$hoursuday.':'.$minutesuday;//Time_Sunday
            $this->db->where('Database_ID',$userid);
            $this->db->where('substr(FromDate,1,10)',$fromdate);
            $this->db->where('substr(ToDate,1,10)',$todate);
            $query20=$this->db->get('Time');
            if($query20->num_rows()==0)
            {
            $insert_time=array(
                'Database_ID'=>$userid,
                'FromDate'=>$fromdate,
                'ToDate'=>$todate,
                'WorkedDay'=>$workedday,
                'WorkingDay'=>$workingday,
                'OT_Weekday'=>$OT_Weekday,
                'OT_Saturday'=>$OT_Saturday,
                'Time_Saturday'=>$timesaturday,
                'Time_Sunday'=>$timesunday
                );
            $inserttime=$this->db->insert('Time',$insert_time);
            return $inserttime;
            }
          
    }
    
    
    
    function getSunLongitude($jdn, $timeZone)
    {
        //var T, T2, dr, M, L0, DL, L;
        $T = ($jdn - 2451545.5 - $timeZone/24) / 36525; // Time in Julian centuries from 2000-01-01 12:00:00 GMT
        $T2 = $T*$T;
        $dr = pi()/180; // degree to radian
        $M = 357.52910 + 35999.05030*$T - 0.0001559*$T2 - 0.00000048*$T*$T2; // mean anomaly, degree
        $L0 = 280.46645 + 36000.76983*$T + 0.0003032*$T2; // mean longitude, degree
        $DL = (1.914600 - 0.004817*$T - 0.000014*$T2)*sin($dr*$M);
        $DL = $DL + (0.019993 - 0.000101*$T)*$Math.sin($dr*2*$M) + 0.000290*sin($dr*3*$M);
        $L = $L0 + $DL; // true longitude, degree
        $L = $L*$dr;
        $L = $L - $PI*2*((INT)($L/(pi()*2))); // Normalize to (0, 2*PI)
        return (INT)($L / pi() * 6);
    }
    function getNewMoonDay($k, $timeZone)
    {
        //var T, T2, T3, dr, Jd1, M, Mpr, F, C1, deltat, JdNew;
        $T = $k/1236.85; // Time in Julian centuries from 1900 January 0.5
        $T2 = $T * $T;
        $T3 = $T2 * $T;
        $dr = pi()/180;
        $Jd1 = 2415020.75933 + 29.53058868*$k + 0.0001178*$T2 - 0.000000155*$T3;
        $Jd1 = $Jd1 + 0.00033*sin((166.56 + 132.87*$T - 0.009173*$T2)*$dr); // Mean new moon
        $M = 359.2242 + 29.10535608*$k - 0.0000333*$T2 - 0.00000347*$T3; // Sun's mean anomaly
        $Mpr = 306.0253 + 385.81691806*$k + 0.0107306*$T2 + 0.00001236*$T3; // Moon's mean anomaly
        $F = 21.2964 + 390.67050646*$k - 0.0016528*$T2 - 0.00000239*$T3; // Moon's argument of latitude
        $C1=(0.1734 - 0.000393*$T)*sin($M*$dr) + 0.0021*sin(2*$dr*$M);
        $C1 = $C1 - 0.4068*sin($Mpr*$dr) + 0.0161*sin($dr*2*$Mpr);
        $C1 = $C1 - 0.0004*sin($dr*3*$Mpr);
        $C1 = $C1 + 0.0104*sin($dr*2*$F) - 0.0051*sin($dr*($M+$Mpr));
        $C1 = $C1 - 0.0074*sin(dr*($M-$Mpr)) + 0.0004*sin($dr*(2*$F+$M));
        $C1 = $C1 - 0.0004*sin(dr*(2*$F-$M)) - 0.0006*sin($dr*(2*$F+$Mpr));
        $C1 = $C1 + 0.0010*sin(dr*(2*$F-$Mpr)) + 0.0005*sin($dr*(2*$Mpr+$M));
        if ($T < -11) {
                $deltat= 0.001 + 0.000839*$T + 0.0002261*$T2 - 0.00000845*$T3 - 0.000000081*$T*$T3;
        } else {
                $deltat= -0.000278 + 0.000265*$T + 0.000262*$T2;
        };
        $JdNew = $Jd1 + $C1 - $deltat;
        return (INT)($JdNew + 0.5 + $timeZone/24);
    }
    function jdToDate($jd)
    {

        //var $a, $b, $c, $d, $e, $m, $day, $month, $year;
        if ($jd > 2299160) { // After 5/10/1582, Gregorian calendar
	$a = jd + 32044;
	$b = INT((4*a+3)/146097);
	$c = $a - INT(($b*146097)/4);
        } 
        else {
	$b = 0;
	$c = $jd + 32082;
        }
        $d = (INT)((4*$c+3)/1461);
        $e = $c - INT((1461*$d)/4);
        $m = (INT)((5*$e+2)/153);
        $day = $e - (INT)((153*$m+2)/5) + 1;
        $month = $m + 3 - 12*INT(m/10);
        $year = $b*100 + $d - 4800 + (INT)($m/10);
        $jdToDate=array($day,$month,$Year);
        return $jdToDate;
    }
    
    function getLeapMonthOffset($a11, $timeZone)
    {
        //var k, last, arc, i;
        $k = (INT)(($a11 - 2415021.076998695) / 29.530588853 + 0.5);
        $last = 0;
        $i = 1; // We start with the month following lunar month 11
        $arc = getSunLongitude(getNewMoonDay($k+i, $timeZone), $timeZone);
        do {
                $last = $arc;
                $i++;
                $arc = getSunLongitude(getNewMoonDay($k+i, $timeZone), $timeZone);
        } while ($arc != $last && $i < 14);
        return $i-1;
    }
    function getLunarMonth11($yy, $timeZone)
    {
        //var k, off, nm, sunLong;
        $off = jdFromDate(31, 12, $yy) - 2415021;
        $k = (INT)($off / 29.530588853);
        $nm = getNewMoonDay($k, $timeZone);
        $sunLong = getSunLongitude($nm, $timeZone); // sun longitude at local midnight
        if ($sunLong >= 9) {
                $nm = getNewMoonDay($k-1, $timeZone);
        }
        return $nm;
    }
    function convertLunarToDate($lunarDay, $lunarMonth, $lunarYear, $lunarLeap, $timeZone)
    {
            //var k, a11, b11, off, leapOff, leapMonth, monthStart;
            if ($lunarMonth < 11) {
                    $a11 = getLunarMonth11($lunarYear-1, $timeZone);
                    $b11 = getLunarMonth11($lunarYear, $timeZone);
            } 
            else 
                {
                    $a11 = getLunarMonth11($lunarYear, $timeZone);
                    $b11 = getLunarMonth11($lunarYear+1, $timeZone);
                }
            $off = $lunarMonth - 11;
            if ($off < 0) {
                $off += 12;
            }
            if ($b11 - $a11 > 365) 
                {
                $leapOff = getLeapMonthOffset($a11, $timeZone);
                $leapMonth = $leapOff - 2;
                if ($leapMonth < 0) {
                    $leapMonth += 12;
                    }
                if ($lunarLeap != 0 && $lunarMonth != $leapMonth) {
                    return array(0, 0, 0);
                    } 
                else if ($lunarLeap != 0 || $off >= $leapOff) {
                    $off += 1;
                    }
            }
            $k = (INT)(0.5 + ($a11 - 2415021.076998695) / 29.530588853);
            $monthStart = getNewMoonDay($k+$off, $timeZone);
            return $this->jdToDate($monthStart+$lunarDay-1);
    }
    public function cutString($string)
    {
        $i=0;
        $count=0;
        while($string[$i]!=' ')
        {
            $count++;
            $i++;
        }
        return $count;
    }
    public function uploadImage($dataImage){ //Thuc hien upload anh
            $_FILES['file_upload'] = $dataImage;
            $file_max_size = 2000; // Tính theo KB
            $folder_upload_in =base_url()."templates/images/upload/"; // Thư mục mà file sẽ được upload vào, để nguyên nếu muốn upload cùng một thư mục
            $file_tmp = isset($_FILES['file_upload']['tmp_name']) ? $_FILES['file_upload']['tmp_name'] : "";
            //$file_tmp = $_FILES['file_upload']['tmp_name'];
            $file_name = isset($_FILES['file_upload']['name']) ? $_FILES['file_upload']['name'] : "";
            //$file_name = $_FILES['file_upload']['name'];
            $file_type = isset($_FILES['file_upload']['type']) ? $_FILES['file_upload']['type'] : "";
            $file_size = isset($_FILES['file_upload']['size']) ? $_FILES['file_upload']['size'] : "";
            $file_error = isset($_FILES['file_upload']['error']) ? $_FILES['file_upload']['error'] : "";
            
            if ( $file_size > ($file_max_size*1024) )
            {
            return false;
            }
            if ( $file_type == "text/plain" )
            {
            return false;
            }
            move_uploaded_file($file_tmp, $folder_upload_in.$file_name);
            //copy( $file_tmp, "./" . $folder_upload_in . $file_name);
           return true;
        }
   
}
?>
